$(function(){
	//输入框输入时
	$('#l_searchInput').keyup(function(){
		//改变搜索图片
		$('.search i img').attr('src','mobile/image/searched.png');
		$('.cancel').css('color','#3B9CE0');
		searchType();
		//客户搜索下选择品种
		$('.slider_con > ul > li').click(function(){
			chioceType.call($(this));
		})
		if($('#l_searchInput').val() == '') {
			deletInput();
		}
	})
	$('#l_searchInput').blur(function(){
		$('.search i img').attr('src','mobile/image/search.png');
		$('.cancel').css('color','#fff');
	})
	
	//客户直接选择品种
	$('#slider_Ul li ul > li').click(function(){
		chioceType.call($(this));
	})
})
//搜索框展示
function searchType() {
    var l_searchInput = $.trim($('#l_searchInput').val());
    if (l_searchInput == "") {
    	$('.slider_con').html('');
		$('.slider_con').hide();
		$('.slider-content').show();
		$('.slider-nav').show();
    }
    if (l_searchInput){
    	//获取种类标签
        $('.enTypename').each(function() {
	      	var typeArr = [];
	      	var str = '';
	      	var enTypename = $(this).html(); //品种英文
	      	var searchValsStr = $.trim($('#l_searchInput').val()); //用户输入的值
	      	if(/^[a-z]+$/.test(searchValsStr)) {
	      		//输入内容为小写字母  转为大写
				searchValsStr = searchValsStr.toUpperCase();
	      	}
	      	if(enTypename.indexOf(searchValsStr) == 0) {
	      		$('.slider-nav').hide();
	      		$('.slider-content').hide();
	      		$(this).parent('li').attr('class','liselected');
	      		$('.slider_con').html($(this).parent().parent().clone());
				
				$('#searchinput').css({'margin-bottom':'1rem'});
				$('.slider_con').show();
	      	}
        });
        $('.zhTypename').each(function() {
	      	var typeArr = [];
	      	var str = '';
	      	var zhTypename = $(this).html(); //品种中文
	      	var searchValsStr = $.trim($('#l_searchInput').val()); //用户输入的值
	      	if(zhTypename.indexOf(searchValsStr) == 0) {
	      		$('.slider-nav').hide(); //字母索引导航隐藏
	      		$('.slider-content').hide(); //展示品种首页隐藏
	      		$(this).parent('li').attr('class','liselected'); //li应用类样式
	      		$('.slider_con').html($(this).parent().parent().clone());
				$('#searchinput').css({'margin-bottom':'1rem'}); //搜索框距离下面区域5rem
				$('.slider_con').show(); //检索出来的展示
	      	}
        });
    }
}

//客户选择品种
function chioceType(){
	//获取品种英文名
		var enTypename = 'FX'+$(this).children('.enTypename').html();
		$.ajax({
			url:gotoAdd,
            type: 'post',
            data: {typename:enTypename},
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            dataType:'json',
			success:function(res){
				if(res.status == 0) { //用户未登录
					showTips();
					$('.l_tips').html('用户未登录');
					hideTips();
					window.location.href = gotoMlogin;
				}else if(res.status == 1) { //信息不正确
					showTips();
					$('.l_tips').html('信息不正确');
					hideTips();
				}else if(res.status == 2) { //品种已存在
					showTips();
					$('.l_tips').html('品种已存在');
					hideTips();
				}else if(res.status == 3) { //添加成功
					showTips();
					$('.l_tips').html('添加成功');
					hideTips();
					window.location.href = gotourl;
				}else if(res.status == 4) { //添加失败
					showTips();
					$('.l_tips').html('添加失败');
					hideTips();
				}
			}
		})
}
function deletInput(){
	//清空input框
	$('.search i img').attr('src','mobile/image/search_append.png');
	$('.cancel').css('color','#FFF');
	$('#l_searchInput').val('');
	$('.slider_con').hide();
	$('.slider-nav').show();
	$('.slider-content').show();
	$('#searchinput').css({'margin-bottom':'0rem'});	
}
//手机搜索事件
$(document).keypress(function (e) {
	if (e.keyCode == 13){
		searchType();
		return false;
	}
});